//
//  WoDeEditeAlertView.m
//  SanYiTeaching
//
//  Created by 李文强 on 2019/4/27.
//  Copyright © 2019 SanYi. All rights reserved.
//

#import "WoDeEditeAlertView.h"
#import "SYRequestAPI+Wode.h"

@interface WoDeEditeAlertView ()
{
    UIView *lineView;
    UIView *lineViewCenter;
    NSString *validKey;
    NSString *phoneNuml;
}
@end

@implementation WoDeEditeAlertView

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
- (void)initSubView{
    self.view.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.4];
    [self.view addSubview:self.baseView];
    [_baseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.view.mas_centerY);
        make.centerX.equalTo(self.view.mas_centerX);
        make.width.equalTo(@300);
        make.height.equalTo(@225);
    }];
//    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismissView)];
//    [self.view addGestureRecognizer:tap];
    
    [_baseView addSubview:self.titleLabel];
    [_baseView addSubview:self.messageLabel];
    [_baseView addSubview:self.enterView];
    [_baseView addSubview:self.yanZhengMaView];
    [_baseView addSubview:self.cancleButton];
    [_baseView addSubview:self.confirmButoon];
    
    lineView = [[UIView alloc]init];
    lineView.backgroundColor = LINCOLOR;
    [_baseView addSubview:lineView];
    
    lineViewCenter = [[UIView alloc]init];
    lineViewCenter.backgroundColor = LINCOLOR;
    [_baseView addSubview:lineViewCenter];
    [self addNoticeForKebord];
}
- (void)dismissView{
    [self dismissViewControllerAnimated:NO completion:nil];
}
- (void)initOption{
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.top.equalTo(@0);
        make.height.equalTo(@40);
    }];
    [_messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.top.equalTo(self->_titleLabel.mas_bottom);
        make.height.equalTo(@13);
    }];
    [_enterView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self->_baseView.mas_centerX);
        make.top.equalTo(@65);
        make.width.equalTo(@250);
        make.height.equalTo(@40);
    }];
    [_yanZhengMaView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self->_baseView.mas_centerX);
        make.top.equalTo(@120);
        make.width.equalTo(@250);
        make.height.equalTo(@40);
    }];
    [_cancleButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.bottom.equalTo(@0);
        make.right.equalTo(self->_baseView.mas_centerX).offset(-0.5);
        make.height.equalTo(@40);
    }];
    [_confirmButoon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self->_baseView.mas_centerX).offset(0.5);
        make.bottom.equalTo(@0);
        make.right.equalTo(self->_baseView.mas_right);
        make.height.equalTo(@40);
    }];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.top.equalTo(@184);
        make.right.equalTo(@0);
        make.height.equalTo(@1);
    }];
    [lineViewCenter mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self->_baseView.mas_centerX);
        make.bottom.equalTo(@0);
        make.width.equalTo(@1);
        make.height.equalTo(@40);
    }];
}
- (WoDeEnterTFView *)enterView{
    if (!_enterView) {
        WoDeEnterTFView *view = [[WoDeEnterTFView alloc]initWithFrame:CGRectMake(0, 0, 200, 50)];
        view.layer.borderColor = LINCOLOR.CGColor;
        view.layer.borderWidth = 1;
        _enterView = view;
    }
    return _enterView;
}
- (WoDeEnterYanZhengMAView *)yanZhengMaView{
    if (!_yanZhengMaView) {
        WoDeEnterYanZhengMAView *view = [[WoDeEnterYanZhengMAView alloc]initWithFrame:CGRectMake(0, 0, 200, 50)];
        view.layer.borderColor = LINCOLOR.CGColor;
        view.layer.borderWidth = 1;
        view.block = ^{
            [self sendYanZhengMaRequest];
        };
        _yanZhengMaView = view;
    }
    return _yanZhengMaView;
}
- (UIView *)baseView{
    if (!_baseView) {
        UIView *view = [[UIView alloc]init];
        view.backgroundColor = [UIColor whiteColor];
        view.layer.cornerRadius = 5;
        view.layer.masksToBounds = YES;
        _baseView = view;
    }
    return _baseView;
}
- (UILabel *)titleLabel{
    if (!_titleLabel) {
        UILabel *label = [[UILabel alloc]init];
        label.text = @"短信验证";
        label.textAlignment = 1;
        label.textColor = [UIColor darkGrayColor];
        label.font = [UIFont systemFontOfSize:14];
        _titleLabel = label;
    }
    return _titleLabel;
}
- (UILabel *)messageLabel{
    if (!_messageLabel) {
        UILabel *label = [[UILabel alloc]init];
        label.text = @"手机号发生变更，需要使用短信验证";
        label.textAlignment = 1;
        label.textColor = TITLECOLOR;
        label.font = [UIFont systemFontOfSize:13];
        _messageLabel = label;
    }
    return _messageLabel;
}
- (UIButton *)cancleButton{
    if (!_cancleButton) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:@"取消" forState:UIControlStateNormal];
        [button setTitleColor:SELECTEDCOLOR forState:UIControlStateNormal];
        button.titleLabel.font  = [UIFont systemFontOfSize:16];
        [button addTarget:self action:@selector(cancleClick:) forControlEvents:UIControlEventTouchUpInside];
        _cancleButton =button;
    }
    return _cancleButton;
}
- (void)cancleClick:(UIButton *)sender{
    self.cancelBlock();
}
-(UIButton *)confirmButoon{
    if (!_confirmButoon) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:@"保存" forState:UIControlStateNormal];
        [button setTitleColor:SELECTEDCOLOR forState:UIControlStateNormal];
        button.titleLabel.font  = [UIFont systemFontOfSize:16];
        [button addTarget:self action:@selector(confirmClick:) forControlEvents:UIControlEventTouchUpInside];
        _confirmButoon =button;
    }
    return _confirmButoon;
}
//保存手机号
- (void)confirmClick:(UIButton *)sender{
    [self changePasswordRequest];
   
}

//- (void)drawRect:(CGRect)rect{
//    UIBezierPath *path = [[UIBezierPath alloc]init];
//    [path moveToPoint:CGPointMake(0, 184)];
//    [path addLineToPoint:CGPointMake(self.view.frame.size.width, 184)];
//    [LINCOLOR set];
//    [path stroke];
//
//
//    UIBezierPath *pathCenter = [[UIBezierPath alloc]init];
//    [pathCenter moveToPoint:CGPointMake(149.5, 184)];
//    [pathCenter addLineToPoint:CGPointMake(149.5, 250)];
//    [LINCOLOR set];
//    [pathCenter stroke];
//}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark - 发送验证码
- (void)sendYanZhengMaRequest{
    phoneNuml = _enterView.enterTF.text;
    [MBProgressHUD mj_showMessage:nil toView:self.view];
    [SYRequestAPI sy_getSafeServiceGetVerifyCode:@{@"mobile":_enterView.enterTF.text,@"verifyType":@"102"} success:^(id  _Nonnull responseObject) {
        NSLog(@"发送验证码成功");
        [MBProgressHUD mj_hideHUDForView:self.view];
        [XSInfoView showInfo:@"发送验证码成功" onSubView:self.view];
    } fail:^(NSError *error) {
        [MBProgressHUD mj_hideHUDForView:self.view];
    }];
}
#pragma mark - 修改手机号
- (void)changePasswordRequest{
    phoneNuml = _enterView.enterTF.text;
    if (phoneNuml.length == 0) {
        [XSInfoView showInfo:@"请完善手机号" onSubView:self.view];
        return;
    }
    validKey = _yanZhengMaView.enterTF.text;
    if (validKey.length == 0) {
        [XSInfoView showInfo:@"请完善验证码" onSubView:self.view];
        return;
    }
    [MBProgressHUD mj_showMessage:nil toView:self.view];
    NSDictionary *parama = @{@"validKey":validKey,
                             @"newMobile":phoneNuml};
    [SYRequestAPI sy_getSafeServiceChangeMobile:parama success:^(id  _Nonnull responseObject) {
        NSLog(@"修改手机号成功 %@",responseObject);
        [XSInfoView showInfo:[responseObject objectForKey:@"msg"] onSubView:self.view];
         self.confirmlBlock();
        [MBProgressHUD mj_hideHUDForView:self.view];
    } fail:^(NSError *error) {
        [MBProgressHUD mj_hideHUDForView:self.view];
    }];
}
#pragma mark - 监控键盘上浮
- (void)addNoticeForKebord{
    //监听事件键盘
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
}
#pragma mark - keyboard

//当键盘出现或改变时调用
- (void)keyboardWillShow:(NSNotification *)note {
    //取出键盘最终的frame
    CGRect rect = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    //取出键盘弹出需要花费的时间
    CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
    CGFloat H = rect.size.height-APP_HEIGHT/2-140;
    [UIView animateWithDuration:duration animations:^{
        [self->_baseView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.view.mas_centerY).offset(H);
        }];
    }];
}

//当键盘消失时调用
- (void)keyboardWillHide:(NSNotification *)note {
    //取出键盘弹出需要花费的时间
    double duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    [UIView animateWithDuration:duration animations:^{
        [self->_baseView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.view.mas_centerY).offset(0);
        }];
    }];
}
@end
